package com.earstep.view
{
	import com.earstep.proxy.LoginProxy;
	import com.erastep.mvc.ifc.INotification;
	import com.erastep.mvc.mediator.Mediator;
	import com.erastep.util.LogUtil;
	import com.erastep.util.StringUtil;
	
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	
	/**
	 * @depart 宁波维涛
	 * @author Jarvis 
	 * @E-mail: shaomj@victopsoft.com
	 * @version 1.0.1 
	 * 创建时间：2011-10-17 下午05:34:41 * 
	 */
	public class LoginViewMediator extends Mediator
	{
		public static const NAME:String = "LoginViewMediator";
		
		private var login:LoginView;
		
		public function LoginViewMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			login = viewComponent as LoginView;
		}
		
		override public function onRegister():void{
			login.addEventListener(KeyboardEvent.KEY_DOWN,submitData);
			login.callLater(function():void{
				login.user.addEventListener(KeyboardEvent.KEY_DOWN,user_keyDownHandler);
				login.pass.addEventListener(KeyboardEvent.KEY_DOWN,pass_keyDownHandler);
				login.loginBtn.addEventListener(MouseEvent.CLICK,btnSubmitData);
				login.focusManager.setFocus(login.user);
				login.focusManager.activate();
			});
		}
		
		public function defaultLogin(obj:Object):void{
			login.user.text = obj.username;
			login.pass.text = obj.password;
			checkLogin();
		}
		
		private function submitData(e:KeyboardEvent):void{
			if(e.keyCode == 13){
				checkLogin();
			}
		}
		
		private function checkLogin():void{
			if(StringUtil.trim(login.user.text) == ""){
				login.user.errorString = "请输入用户名!";
			}
			if(StringUtil.trim(login.pass.text) == ""){
				login.pass.errorString = "请输入密码!";
			}
			if(StringUtil.trim(login.user.text) != "" && StringUtil.trim(login.pass.text) != ""){
				login.loadingsbar.visible = true;
				login.loginEffect.play();
				retriveProxy(LoginProxy.NAME).callFunction("checkUser",{userName:login.user.text,passWord:login.pass.text});
			}
		}
		
		public function stopEffect(e:Object):void{
			login.loginEffect.stop();
			//login.loadingsbar.visible = false;
			//login.load_label.visible = false;
		}
		
		/**
		 * 点击提交按钮触发
		 * */
		private function btnSubmitData(e:MouseEvent):void{
			checkLogin();
		}
		
		public function respondToLoginBySession(note:INotification):void{
			LogUtil.print("开始通过SessionID和usercode登录.....");
			login.loginEffect.play();
			login.loadingsbar.visible = true;
			retriveProxy(LoginProxy.NAME).callFunction("checkSessionId",note.getBody());
		}
		
		/**
		 * login system fail
		 * */
		public function respondToLoginFail(note:INotification):void{
			login.loginEffect.stop();
			login.loadingsbar.visible = false;
			//移除Login的Mediator。
			//Alert.show("无法连接到服务器,请确认当前的访问路径!");
		}
		public function respondToLoginFailMessage(note:INotification):void{
			login.loginEffect.stop();
			//移除Login的Mediator。
			login.loadingsbar.visible = false;
			Alert.show(note.getBody().message,"message",4,login);
		}
		/**
		 * 获取成功
		 * */
		//private function 
		
		protected function user_keyDownHandler(event:KeyboardEvent):void
		{
			// TODO Auto-generated method stub
			if(StringUtil.trim(login.user.text) != ""){
				login.user.errorString = "";
			}
		}
		
		protected function pass_keyDownHandler(event:KeyboardEvent):void
		{
			// TODO Auto-generated method stub
			if(StringUtil.trim(login.pass.text) != ""){
				login.pass.errorString = "";
			}
		}
		
		override public function onRemove():void{
			login.loginEffect.stop();
		}		
		
	}
}